// miniprogram/pages/player/player.js
let musiclist = []
let nowPlayingIndex = 0
let backgroundAudioManager = wx.getBackgroundAudioManager()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: '',
    isPlaying: false,
    isLyricShow: false, //表示当前歌词是否显示
    lycir: '',
    isSame: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /*     console.log(options, 'options') */
    nowPlayingIndex = options.index
    musiclist = wx.getStorageSync('musiclist')
    this.loadMusicDetail(options.musicid)

  },

  loadMusicDetail(musicid) {
    if (musicid == app.getPlayMusicId()) {
      this.setData({
        isSame: true
      })
    }
    this.setData({
      isSame: false
    })
    if (this.data.isSame) {
      backgroundAudioManager.stop()
    }


    let music = musiclist[nowPlayingIndex]
    /*     console.log(music, 'music') */
    wx.setNavigationBarTitle({
      title: music.name
    })
    this.setData({
      picUrl: music.al.picUrl,
      isPlaying: false
    })
    app.setPlayMusicId(musicid)
    wx.showLoading({
      title: '加载中',
    })
    /*  console.log(music.al.picUrl) */
    /*    console.log(this.data.picUrl) */
    wx.cloud.callFunction({
      name: 'music',
      data: {
        musicId: musicid,
        $url: 'musicUrl'
      }
    }).then((res) => {
      /* const backgroundAudioManager = wx.getBackgroundAudioManager() */
      /*    console.log(res.result, 'res.result') */
      let rr = res.result
      if (rr.data[0].url == null) {
        wx.showToast({
          title: '无权限播放',
        })
        return
      }
      if (!this.data.isSame) {
        backgroundAudioManager.src = rr.data[0].url
        backgroundAudioManager.title = music.name
        backgroundAudioManager.coverImgUrl = music.al.picUrl
        backgroundAudioManager.singer = music.ar[0].name
        backgroundAudioManager.epname = music.al.name
        this.savePlayHistory()
      }

      /*    console.log(rr.data[0].url, 'url') */

      this.setData({
        isPlaying: true,
      })
      wx.hideLoading()
      wx.cloud.callFunction({
        name: 'music',
        data: {
          $url: 'lyric',
          musicId: musicid
        }
      }).then((res) => {
        console.log(res, 'lyric')
        let lyric = '暂无歌词'
        const lrc = res.result.lrc
        if (lrc) {
          lyric = lrc.lyric
        }
        this.setData({
          lyric,
        })
      })
    })

  },
  togglePlaying() {
    if (this.data.isPlaying) {
      /*   console.log('真在播放') */
      backgroundAudioManager.pause()
    } else
      backgroundAudioManager.play()
    this.setData({
      isPlaying: !this.data.isPlaying
    })
  },
  onPrev() {
    nowPlayingIndex--
    if (nowPlayingIndex < 0) {
      nowPlayingIndex = musiclist.length - 1
    }
    this.loadMusicDetail(musiclist[nowPlayingIndex].id)
  },
  onNext() {
    nowPlayingIndex++
    if (nowPlayingIndex === musiclist.length) {
      nowPlayingIndex = 0
    }
    this.loadMusicDetail(musiclist[nowPlayingIndex].id)
  },
  onChangeLyricShow() {
    this.setData({
      isLyricShow: !this.data.isLyricShow
    })
  },
  timeUpdate(event) {
    this.selectComponent('.lyric').update(event.detail.currentTime)
  },
  savePlayHistory() {
    //当前真在播放的歌曲
    const music = musiclist[nowPlayingIndex]
    const openid = app.globalData.openid
    const history = wx.getStorageSync(openid)
    let bHave = false
    for (let i = 0, len = history.length; i < len; i++) {
      if (history[i].id == music.id) {
        bHave = true
        break
      }
    }
    if (!bHave) {
      history.unshift(music)
      wx.setStorage({
        data: history,
        key: openid,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onPlay() {
    this.setData({
      isPlaying: true
    })
  },
  onPause() {
    this.setData({
      isPlaying: false
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})